/*
 * Cashbook Gadget
 *
 * Copyright (c) 2010 Smart Infotec Inc. (smart-infotec.com)
 * Licensed under the GPL (GPL-LICENSE.txt) licenses.
 *
 */

// debug flag. When its value is non-zero, debugging messages are displayed      
var debug = 1;
// The string containing debugging messages
var debug_html = "";

// Get userprefs
var prefs = new gadgets.Prefs();
var what_order_by = prefs.getString("what_order_by");
var rows_per_page = prefs.getInt("rows_per_page");

// In the constructor, state that messages should be be displayed
// in the messageBox <div> rather than at the top of the gadget.
var minimsg = new gadgets.MiniMessage(prefs.getModuleId(), document.getElementById("messageBox"));

function makeDOMRequest() {
  var params = {};
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
  // params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
  // var url = "http://xutao1976-engineapp.appspot.com/show_all.xml";
  var url = "http://jbobos.googlecode.com/svn/trunk/gadget/cashbook.xml"
  gadgets.io.makeRequest(url, response, params);
};

function response(obj) {
  // Start building HTML string that will be displayed in <div>.
  var html = "<div>"
  html += "<TABLE cellspacing='0' border='1' align='center'>";
  html += "<CAPTION>My Cashbook</CAPTION>";
  html += "<THEAD valign='top'>";
  html += "<TR><TH>Date</TH><TH>Summary</TH><TH>Incomes</TH><TH>Expenses</TH></TR>";
  html += "<TBODY>";

  // obj.data contains a Document DOM element corresponding to the page that was requested
  var domdata = obj.data;
  if (obj.errors != "") {
    print("Can not parse xml data : " + obj.errors);
    return -1;
  }

  var itemList = domdata.getElementsByTagName("entity");
  //print("We have "+itemList.length+" entities.<br>");
  minimsg.createDismissibleMessage("We have "+itemList.length+" entities.<br>");

  // Loop through all <entity> nodes
  for (var i = 0; i < itemList.length ; i++) {
    if (itemList.item(i).nodeType != 1) continue; //Only process element type

    var nodeList = itemList.item(i).childNodes;
    for (var j = 0; j < nodeList.length ; j++) {
      if (nodeList.item(j).nodeType != 1) continue; //Only process element type

      var node = nodeList.item(j);
      if (node.getAttribute("name") == "issue_date") { var issue_date = node.firstChild.nodeValue; }
      if (node.getAttribute("name") == "summary") { var summary = node.firstChild.nodeValue; }
      if (node.getAttribute("name") == "incomes") { var incomes = node.firstChild.nodeValue; }
      if (node.getAttribute("name") == "expenses") { var expenses = node.firstChild.nodeValue; }
    }
    html += "<TR><TD>"+issue_date+"</TD><TD>"+summary+"</TD><TD>"+incomes+"</TD><TD>"+expenses+"</TD></TR>";
  }
  html += "</TABLE>";
  html += "</div>";
  document.getElementById('content_div').innerHTML = html;
}; //end of func.response

// Outputs debug messages if debug flag has a non-zero value
function print(msg) {      
  if (debug) {
    debug_html += msg;
    // Write debug HTML to div
    document.getElementById("debug_div").innerHTML = debug_html;
  }
};

gadgets.util.registerOnLoadHandler(makeDOMRequest);
